#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#include"wav.h"
typedef struct {
	unsigned char name[4];
	unsigned int len;
} chunk_t;

int write_wav_head(int fd, WAV_t * wav)
{
	if (fd < 0)      printf("Error: fd = %d \n", fd);
	if (wav == NULL) printf("Error: wav is NULL \n");

	wav->fact.Len = 16;
	wav->fact.Data = NULL;
	if (write(fd, &wav, sizeof(WAV_t)) < sizeof(WAV_t))
		printf("write RIFF error!\n");

	printf("write wav head success!\n");
	return 0;
}

int read_wav_head(int fd, WAV_t * wav)
{
	if (fd < 0)     printf("Error: fd = %d \n", fd);
	if(wav == NULL) printf("Error: wav is NULL \n");

	wav->fact.Data = NULL;

	if (read(fd, &wav->riff, sizeof(RIFF_t)) < sizeof(RIFF_t))
		printf("read RIFF error!\n");
	if (read(fd, &wav->Idw, 4) < 4)
		printf("read WAVE error!\n");
	if (read(fd, &wav->fmt, sizeof(FMT_t)) < sizeof(FMT_t))
		printf("read FMT error!\n");
	/*
	if (wav->fmt.Len != 16) {
		if (read(fd, &wav->fact, 8) < 8)
			printf("read fact head error!\n");
		wav->fact.Data = (u_char*) malloc(wav->fact.Len);
		if (wav->fact.Data == NULL)
			printf("read fact buf error!\n");
		if (read(fd, &wav->fact.Data, wav->fact.Len) < wav->fact.Len)
			printf("read fact data error!\n");
	}
	*/
	read(fd, &wav->Idd, 2);
	if(read(fd, &wav->Idd, 8) < 8) //read Idd and Len
		printf("read data flag error!\n");
	printf("read wav head success!\n");
	return 0;	
}

int printf_wav_head_en(WAV_t wav)
{
	//printf
	printf("\n--------------------------------------------------\n");
	printf("id:%.4s\n"                  ,wav.riff.Id);
	printf("\tLen:           %d\n\n"    ,wav.riff.Len);
	printf("id:%.4s\n\n"                ,wav.Idw);
	printf("Id:%.4s\n"                  ,wav.fmt.Id);
	printf("\tLen:           %d\n"      ,wav.fmt.Len);
	printf("\tEncodeType:    %d\n"      ,wav.fmt.EncodeType);
	printf("\tChannels:      %d\n"      ,wav.fmt.Channels);
	printf("\tSampleRate:    %d\n"      ,wav.fmt.SampleRate);
	printf("\tByteRate:      %d\n"      ,wav.fmt.ByteRate);
	printf("\tBlockAlign:    %d\n"      ,wav.fmt.BlockAlign);
	printf("\tBitsPerSample: %d\n\n"    ,wav.fmt.BitsPerSample);
	/*
	if(wav.fmt.Len != 16) {
		printf("Id:%.4s\n"              ,wav.fact.Id);
		printf("\tLen:  %d\n"           ,wav.fact.Len);
		printf("\tData: %s\n\n"         ,wav.fact.Data);
	} else {
		printf("NO HAVE FACT..\n\n");
	}
	*/
	printf("Id:%.4s\n"                  ,wav.Idd);
	printf("\tLen: %d\n\n"              ,wav.Len);
	printf("--------------------------------------------------\n\n");
	return 0;
}


int printf_wav_head_cn(WAV_t wav)
{
	printf("\n--------------------------------------------------\n");
	printf("标志:%.4s\n"                  ,wav.riff.Id);
	printf("\t长度:        %d\n\n"        ,wav.riff.Len);
	printf("标志:%.4s\n\n"                ,wav.Idw);
	printf("标志:%.4s\n"                  ,wav.fmt.Id);
	printf("\t长度:        %d\n"          ,wav.fmt.Len);
	printf("\t编码类型:    %d\n"            ,wav.fmt.EncodeType);
	printf("\t通道数:      %d\n"           ,wav.fmt.Channels);
	printf("\t采样频率:    %d\n"            ,wav.fmt.SampleRate);
	printf("\t传输速率:    %d\n"            ,wav.fmt.ByteRate);
	printf("\t块对齐:      %d\n"           ,wav.fmt.BlockAlign);
	printf("\t量化位数:    %d\n\n"          ,wav.fmt.BitsPerSample);
	/*
	if (wav.fmt.Len != 16) {
		printf("标志:%.4s\n"              ,wav.fact.Id);
		printf("\t长度:    %d\n"          ,wav.fact.Len);
		printf("\t数据:    %s\n\n"        ,wav.fact.Data);
	} else {
		printf("没有FACT..\n\n");
	}
	*/
	printf("标志:%.4s\n"                  ,wav.Idd);
	printf("\t长度:         %d\n\n"       ,wav.Len);
	printf("--------------------------------------------------\n\n");
	return 0;
}
